#pragma once

#include "Object.h"

class GrassBlade : public Object
{
private:
	float windBlow;
	float windTheta;
	float straightness;
	float green;
	Vector3 normal;

	void setColorMaterial()
	{
		GLfloat mat_ambient[] = {0.0f, 0.50f, 0.0f, 1.0f};
		GLfloat mat_diffuse[] = {0.0f, green, 0.0f, 1.0f};
		GLfloat mat_specular[] = {0.0f, 0.0f, 0.0f, 0.0f};
		GLfloat mat_shininess[] = {0.0f};
		glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
		glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
		glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
		glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
	}

public:
	GrassBlade()
	{
		windBlow = 2;
		windTheta = 0;
		rotation = rand()%360;	// Randomness in positional appearance
		straightness = rand()%30;	// Randomness in how straight it stands (90 - 60) degrees from ground
		green = 0.50f + ((rand()/(float)RAND_MAX)/5); // A bit of randomness in color
	}
	
	void setNormal(Vector3 normal)
	{
		this->normal = normal;
	}

	void draw()
	{
		glPushMatrix();

		// Transformations
		glTranslated(position.x, position.y, position.z);
		glScaled(size,size,size);
		glRotatef(rotation, 0, 1, 0);
		glRotatef(straightness + sin(windTheta), 1, 0, 0);

		setColorMaterial();
		glBegin(GL_POLYGON);
			//glNormal3f(0, 0, 1);
			glNormal3f(normal.getX(), normal.getY(), normal.getZ());
			glVertex3d(0.04, 0.0, 0.0);
			glVertex3d(0.03, 0.9, 0.0);
			glVertex3d(0.0, 1.0, 0.0);
			glVertex3d(-0.03, 0.9, 0.0);
			glVertex3d(-0.04, 0.0, 0.0);
		glEnd();

		glPopMatrix();
	}

	void update(float delta)
	{
		windTheta += windBlow * delta;
		if (windTheta >= PI*2) { windTheta = 0; }
	}
};
